{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regular (dense) voxels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "from math import sqrt, sin, cos\n",
    "\n",
    "width = 100\n",
    "height = 100\n",
    "length = 100\n",
    "\n",
    "def r(x, y, z):\n",
    "    r = sqrt((x - width / 2) * (x - width / 2) + (y - height / 2) * (y - height / 2) + (z - length / 2) * (z - length / 2))\n",
    "    r += sin(x / 2) * 3\n",
    "    r += cos(y / 10) * 5\n",
    "    \n",
    "    return r\n",
    "\n",
    "def f(x, y, z):\n",
    "    return 0 if r(x, y, z) > width / 2 else (1 if y + sin(x / 20) * 10 > height / 2 else 2)\n",
    "\n",
    "color_map = (0xffff00, 0xff0000)\n",
    "voxels = [[[f(x, y, z) for x in range(width)] for y in range(height)] for z in range(length)]\n",
    "\n",
    "# add red voxels in the corners, just for \n",
    "for x in [0, -1]:\n",
    "    for y in [0, -1]:\n",
    "        for z in [0, -1]:\n",
    "            voxels[z][y][x] = 2\n",
    "\n",
    "plot = k3d.plot()\n",
    "obj = k3d.voxels(voxels, color_map, compression_level=1)\n",
    "plot += obj\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.objects[0].wireframe=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.objects[0].wireframe=False\n",
    "plot.objects[0].outlines=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.objects[0].outlines_color=255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.screenshot_scale = 3.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.objects[0].opacity = 0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sparse voxels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "N = 20\n",
    "\n",
    "sparse_voxels = np.random.randint(0, 5, size=(N, 4), dtype=np.uint16)\n",
    "sparse_voxels[:, 3] = np.random.randint(1, 3, size=(N,))\n",
    "sparse_voxels[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default color_map used\n",
    "plot = k3d.plot()\n",
    "obj = k3d.sparse_voxels(sparse_voxels, [10, 10, 10], compression_level=1)\n",
    "plot += obj\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
